<?php
	include_once('Conexion.php');
	
	class Querys{
	
		private $conexion;
		
		public function Querys(){
			$conexion = new Conexion();
			$conexion->conectar();
			$conexion->select();
		}		
	
		/*HACE LA CONSULTA MYSQL QUE LE PASE COMO ARGUMENTO*/
		
		protected function consulta($sql){
			$result = mysql_query($sql);
			if(!$result){
				echo "Invalid Query: ". mysql_error();
			}else{
				return $result;
			}
		}
		
		protected function fetch($result){
			return mysql_fetch_assoc($result);
		}
		
		protected function num_rows($result){
			return mysql_num_rows($result);
		}
		
		public function getAll($orden = null, $from = 0, $limite = 5){
			if($orden){
				return $this->consulta("SELECT * FROM ".$this->tabla." ORDER BY $orden");
			}else{
				return $this->consulta("SELECT * FROM ".$this->tabla);
			}
		}
		
		public function getById($id){
			return $this->consulta("SELECT * FROM ".$this->tabla." WHERE id=$id");
		}
		
		public function getByName($name){
			return $this->consulta("SELECT * FROM ".$this->tabla." WHERE nombre='$name'");
		}
		
		public function getCantidad($resultados = 5){
			$result = $this->consulta("SELECT COUNT($this->tabla.id) AS cantidad FROM $this->tabla");
			
			$fila = $this->fetch($result);
			
			return ceil($fila["cantidad"]/$resultados);
		}
		
		public function save($values){
			if($this->consulta("INSERT INTO ".$this->tabla." VALUES (null, $values)")){
				return true;
			}else{
				return false;
			}
		}
		
		public function update($id, $values){
			if($this->consulta("UPDATE ".$this->tabla." SET $values WHERE id=$id")){
				return true;
			}else{
				return false;
			}
		}
		
		public function delete($id){
			if($this->consulta("DELETE FROM ".$this->tabla." WHERE id=$id")){
				return true;
			}else{
				return false;
			}
		}
	}

?>